package com.xjrsoft.module.iot.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.model.result.R;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.iot.dto.IotGroupPageDto;
import com.xjrsoft.module.iot.entity.IotGroup;
import com.xjrsoft.module.iot.service.IIotGroupService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tzx
 * @since 2024-04-10
 */
@RestController
@RequestMapping(GlobalConstant.IOT_MODULE_PREFIX + "/group")
@AllArgsConstructor
public class IotGroupController {

    private final IIotGroupService iotGroupService;

    @GetMapping("/list")
    @ApiOperation(value = "获取数据（不分页）")
    public R list() {

        List<IotGroup> list = iotGroupService.list(Wrappers.lambdaQuery(IotGroup.class)
                .select(IotGroup.class, x -> VoToColumnUtil.fieldsToColumns(IotGroup.class).contains(x.getProperty())));
        return R.ok(list);
    }


    @GetMapping(value = "/page")
    @ApiOperation(value="查询分组 (分页)")
    public R page(@Valid IotGroupPageDto dto){

        LambdaQueryWrapper<IotGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(StrUtil.isNotBlank(dto.getKeyword()),IotGroup::getName,dto.getKeyword())
                .orderByDesc(IotGroup::getId)
                .select(IotGroup.class,x -> VoToColumnUtil.fieldsToColumns(IotGroup.class).contains(x.getProperty()));


        IPage<IotGroup> page = iotGroupService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<IotGroup> pageOutput = ConventPage.getPageOutput(page, IotGroup.class);
        return R.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id 分组详情")
    public R info(@RequestParam Long id){
        IotGroup group = iotGroupService.getById(id);
        if (group == null) {
            return R.error("找不到此数据！");
        }
        return R.ok(group);
    }


    @PostMapping
    @ApiOperation(value = "新增 分组")
    @Transactional(rollbackFor = Exception.class)
    public R add(@Valid @RequestBody IotGroup dto){

        iotGroupService.save(dto);
        return R.ok(dto.getId());

    }

    @PutMapping
    @ApiOperation(value = "修改分组")
    public R update(@Valid @RequestBody IotGroup dto){

        return R.ok(iotGroupService.updateById(dto));

    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    public R delete(@Valid @RequestBody List<Long> ids){
        return R.ok(iotGroupService.removeBatchByIds(ids));

    }
}
